package com.tang.consumer.netty.codec.json;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Tang
 * @classname JSONEncoder
 * @description [ TODO ]
 * @date 2021/1/19 23:20
 */
@Slf4j
public class JSONEncoder extends MessageToByteEncoder<ProtocolMessage> {

    @Override
    protected void encode(ChannelHandlerContext ctx, ProtocolMessage msg, ByteBuf out) throws Exception {
        log.info("编码器执行...");
        Integer msgLen = msg.getLen();
        out.writeInt(msgLen);
        out.writeBytes(msg.getMessage());
    }
}